package demo.expression;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import demo.zandroid.R;

/**
 * @author hezy
 */
public class ExpressionAdapter extends BaseAdapter {
	private ArrayList<String> imageList;
	private Context context;
	private ExpressionUtil expressionUtil;

	public ExpressionAdapter(Context context, ArrayList<String> imageList) {
		this.context = context;
		this.imageList = imageList;
		expressionUtil = ExpressionUtil.getInstance(context);
	}

	@Override
	public int getCount() {
		return imageList.size();
	}

	@Override
	public Object getItem(int position) {
		return imageList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View v, ViewGroup parent) {
		ViewHolder holder;
		if (v == null) {
			v = LayoutInflater.from(context).inflate(
					R.layout.expression_text_item, null);
			holder = new ViewHolder();
			holder.text = (TextView) v.findViewById(R.id.text_expressionitem);
			v.setTag(holder);
		}
		String content = imageList.get(position);
		holder = (ViewHolder) v.getTag();
		if (content != null) {
			holder.text.setText(content);
			expressionUtil.setExpression(holder.text);
		} else {
			holder.text.setText("[删除]");
			SpannableStringBuilder sBuilder = new SpannableStringBuilder(
					holder.text.getText());

			int size = (int) (ExpressionUtil.SCALE * holder.text.getTextSize());
			Drawable drawable = context.getResources().getDrawable(
					R.drawable.exp_del_nor);
			drawable.setBounds(0, 0, size, size);
			ImageSpan span = new ImageSpan(drawable);

			sBuilder.setSpan(span, 0, holder.text.getText().length(),
					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			holder.text.setText(sBuilder);
		}
		return v;
	}

	private class ViewHolder {
		TextView text;
	}
}
